package lib.Dao;

import lib.Model.Course;
import lib.Model.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.ArrayList;
import lib.Dao.CourseDAO;
import lib.util.PostGetNewMessage;

public class UserDao {
    public void useradd(String openid,String cid) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        List<User> list=new ArrayList<>();

         try{
            con = dbutil.getCon();
            stmt = con.createStatement();
            String sql = "select * from user where openid like "+"'"+openid+"'";
            rs = stmt.executeQuery(sql);
            if(!rs.next()) {
                String sql1 = "insert into user (openid,count,cid1) values ("+"'"+openid+"'"+","+"1"+","+"'"+cid+"'"+")";
                stmt.executeUpdate(sql1);

            }else {
                int count=0;
                    if(rs.getString("cid1")==null){
                        count=1;
                    }else {
                        count = rs.getInt("count") + 1;
                    }
                    if(count<11) {
                        String sql2 = "update user set cid" + count + "=" + "'" + cid + "'" + "," + " count=" + count + " where openid like " + "'" + openid + "'";
                        stmt.executeUpdate(sql2);
                    }
            }
            return;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ;
    }

    public void setUserSe(String openid,String se) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        try {
            con = dbutil.getCon();
            stmt = con.createStatement();
            String sql2 = "update user set se=" + "'" + se + "'"  + " where openid like " + "'" + openid + "'";
            stmt.executeUpdate(sql2);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public String getUserSe(String openid) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        String se="";
        try {

            con = dbutil.getCon();
            stmt = con.createStatement();
            String sql = "select * from user where openid like "+"'"+openid+"'";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                se=rs.getString("se");
            }
            return se;
            } catch (Exception e) {
            e.printStackTrace();
        }
        return se;
    }

    public List<Course> usercourse(String se,String openid) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        List<Course> list =new ArrayList<>();

        try{
            con = dbutil.getCon();
            stmt = con.createStatement();
            String sql = "select * from user where openid like "+"'"+openid+"'";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                int count=rs.getInt("count");
                for(int i=1;i<=count;i++){
                    String cid=rs.getString("cid"+i);
                    CourseDAO courseDAO=new CourseDAO();
                    Course course=courseDAO.getcoursefromCid(se,cid);
                    list.add(course);

                }
            }
            return list;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    public List<Course> usercourseDelete(String openid,String courseid) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        List<Course> list =new ArrayList<>();

        try{
            con = dbutil.getCon();
            stmt = con.createStatement();
            String sql = "select * from user where openid like "+"'"+openid+"'";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                int count=rs.getInt("count");
                int location=0;
                for(int i=1;i<=count;i++){
                    String cid=rs.getString("cid"+i);
                    if(cid.equals(courseid)){
                        location=i;
                    }
                }
                for(int i=location;i<=count;i++){
                    String sql1 = "select * from user where openid like "+"'"+openid+"'";
                    rs = stmt.executeQuery(sql1);
                    String nextcourse="";
                    while (rs.next()) {
                        nextcourse=rs.getString("cid"+(i+1));
                    }

                    String sql2 = "update user set cid" +i+ "=" + "'" + nextcourse + "'"+","+"cid" +(i+1)+ "=" + "NULL" + "," + " count=" + (count-1) + " where openid like " + "'" + openid + "'";
                    stmt.executeUpdate(sql2);

                }
            }
            return list;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public void preuseradd(String openid,String cid) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        List<User> list=new ArrayList<>();

        try{
            con = dbutil.getCon();
            stmt = con.createStatement();
            String sql = "select * from user where openid like "+"'"+openid+"'";
            rs = stmt.executeQuery(sql);
            if(!rs.next()) {
                String sql1 = "insert into user (openid,getnew,precourse) values ("+"'"+openid+"'"+","+"0"+","+"'"+cid+"'"+")";
                stmt.executeUpdate(sql1);

            }else {

                    String sql2 = "update user set precourse=" + "'" + cid + "'" + "," + " getnew=" + 0 + " where openid like " + "'" + openid + "'";
                    stmt.executeUpdate(sql2);

            }
            return;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ;
    }

    public void preusercountadd(String openid) throws SQLException{
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;
        List<User> list=new ArrayList<>();
        CourseDAO courseDAO=new CourseDAO();
        UserDao userDao=new UserDao();
        try{
            con = dbutil.getCon();
            stmt = con.createStatement();
            String sql = "select * from user where openid like "+"'"+openid+"'";
            rs = stmt.executeQuery(sql);
            int count = 0;
            String cid="";

            while (rs.next()) {
                count=rs.getInt("getnew");
                cid=rs.getString("precourse");
            }
            System.out.println(count);
            count=count+1;
        if(count>=2){
            CourseUserDAO courseUserDAO=new CourseUserDAO();
            if(cid!=null)
            useradd(openid,cid);
            courseUserDAO.CourseUseradd(cid,openid);
            courseDAO.SetCcount(userDao.getUserSe(openid),cid);

            String sql2 = "update user set getnew=" + 0 + ", precourse=" + "NULL" + " where openid like " + "'" + openid + "'";
            stmt.executeUpdate(sql2);
            PostGetNewMessage postGetNewMessage=new PostGetNewMessage();
            
            String sql3 = "select * from "+"hdu"+" where cid like  "+"'"+cid+"'";

            rs =stmt.executeQuery(sql3);
            String coursedata = "";
            while(rs.next()) {
                coursedata= "课程名称："+rs.getString("cname")+"   授课老师："+rs.getString("cteacher")+"   上课时间："+rs.getString("ctime");
                        
            }
            
            postGetNewMessage.posttoopenid(openid,coursedata);
        }else {
            String sql2 = "update user set getnew=" + count +" where openid like " + "'" + openid + "'";
            stmt.executeUpdate(sql2);
        }

            return;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ;
    }



}
